home *** CD-ROM | disk | FTP | other *** search
- Path: in1.uu.net!novia!usenet
- From: srwillrd@novia.net (William E. Kempf)
- Newsgroups: comp.lang.c++,comp.os.ms-windows.programmer.misc
- Subject: Re: Virtual function question
- Date: Tue, 02 Apr 1996 19:27:02 GMT
- Organization: Novia Internetworking <> 28.8kbps dialup; 402/390-2NET
- Message-ID: <316178f3.1131475816@204.248.25.97>
- References: <4jp6e9$ou5@dub-news-svc-1.compuserve.com>
- NNTP-Posting-Host: 167.16.65.84
- X-Newsreader: Forte Agent .99e/32.194
-
- RossBoylan@aol.com (Ross Boylan) wrote:
-
- :I am trying to define, a la Smalltalk, a mix-in which defines the
- :various comparison operators in terms of one fundamental operator, <.
- :
- :
- :typedef int bool;
- :class RBMagnitude {
- :public:
- ://subclass should redefine < with appropriate types
- : virtual bool operator<(const RBMagnitude&) const= 0;
- : virtual bool operator>(const RBMagnitude& x) const {return *this <
- :x;};
- :// and many more
- :};
- :
- :class A : public RBMagnitude {
- :public:
- : virtual bool operator<(const A& a) const {return (this->n < a.n);};
- :private:
- : int n;
- :};
- :
- :
- :class A test;
- :
- :This fails in MSVC++ 4.0 with the error that A is virtual because the
- :operator<(const RBMagnitude&) is not defined.
- :It looks to me as if this is because the operator< for A has a
- :different type signature (it uses an A rather than an RBMagnitude).
- :If so, the problem is the language definition, not Microsoft's
- :implementation.
- :
- :1) Is this interpretation correct?
- :2) Is there any way around this problem? Commenting out the
- :definition of RBMagnitude::operator< doesn't work, because
- :RBMagnitude::operator> requires it. I suppose I could define a bogus
- :operator< which throws an exception, but this seems awkward (in
- :particular, if A and B are both RBMagnitudes and I ask A<B, I fail at
- :run time rather than compile time).
- :
- :I would appreciate e-mail; I'll summarize here.
- :
-
- Well, I have a few corrections. This can be done in C++, in fact
- Borland used to have this defined for their base object class in their
- class libraries.
-
- Firstly, there is no way that I know of to define all the "equality"
- operators off of just one (it requires two of them). Secondly, your >
- operator is returning the value for <. Serious bug. Thirdly, do
- *NOT* typedef a bool type! This will soon be a keyword in the C++
- language, and will likely break your code.
-
- Finally, yes the parameters have to be the same type. You'll have to
- define A::operator<(RBMagnitude) and within this function you will use
- the IsKindOf() method (assuming you are using MFC and CObject
- derivatives, otherwise, use the new RTTI operators) to verify the
- RBMagnitude passed in is an A class, then do the comparisons. The
- other overloaded operators will call this one and will thus accomplish
- exactly what you want.
-
- One final note, though: do not make the other "equality" operators
- virtual. The whole purpose is to not have to overload them for the
- new class, so there is no reason for them to be virtual.
-
- Proper implementation:
-
- class RBMagnitude
- {
- virtual int operator ==(RBMagnitude& x) const = 0;
- virtual int operator <(RBMagnitude& x) const = 0;
- int operator >(RBMagnitude& x) { return !(*this == x || *this<x);}
- int operator <=(RBMagnitude& x) { return (*this == x || *this<x);}
- int operator >=(RBMagnitude& x) { return !(*this < x); }
- int operator ==(RBMagnitude& x) { return !(*this == x); }
- };
-
- class A : public RBMagnitude, public CObject
- {
- virtual int operator ==(RBMagnitude& x)
- { if (x.IsKindOf(RUNTIME_CLASS(A)) return n == x.n;
- else return 0; }
- virtual int operator <(RBMagnitude& x)
- { if (x.IsKindOf(RUNTIME_CLASS(A)) return n < x.n;
- else return 0; }
- };
- -----
- William E. Kempf : http://www.novia.net/~srwillrd
- "Sir Willard" : mailto:srwillrd@novia.net (home)
- Knight of the Ascii Table : mailto:wekempf@marlton.1dc.com (work)
-